package cn.com.infcn.monitor.task.sql;

import java.util.List;
import java.util.Map;

import com.alibaba.druid.stat.DruidStatManagerFacade;

import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.SqlUtils;

public class SqlTask implements BaseTask {

	private static DruidStatManagerFacade statManagerFacade = DruidStatManagerFacade.getInstance();
	
	@Override
	public Object execute() throws Exception {
		return sql();
	}

	public Object sql() {
		try {
			List<Map<String, Object>> array = statManagerFacade.getSqlStatDataList(null);
			if(array==null || array.isEmpty()){
				return null;
			}
	        List<Map<String, Object>> sortedArray = SqlUtils.comparatorOrderBy(array, null);
	        for (Map<String, Object> map : sortedArray) {
				for (String key : map.keySet()) {
					Object o = map.get(key);
					if(o instanceof String){
						map.put(key, replaceString(map.get(key)));
					}
				}
			}
	        return sortedArray;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private String replaceString(Object obj){
		if(obj==null){
			return null;
		}
		return obj.toString().replace("\t", "").replace("\n", "");
	}
}
